package com.service.lotterySystem.controller;

import com.service.config.utils.JsonXMLUtils;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;
import com.service.lotterySystem.dto.HelpUserInfoDto;
import com.service.lotterySystem.dto.UserDto;
import com.service.lotterySystem.service.HelpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.service.config.constant.Constant.*;

/**
 * @author ：hjl
 * @date ：Created in 2022/01/14 14:38
 * @description：
 */
@RestController
@RequestMapping("helpController")
@Api(value = "助力接口", tags = "助力接口")
public class HelpController {
    @Resource
    private HelpService helpService;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 助力
     * @param
     * @return
     */
    @PostMapping(value = "help",produces="application/json")
    @ApiOperation(value = "助力")
    @ResponseBody
    public Result addActivity(@RequestHeader(value = "cloud-Auth-Token",required = false) @ApiParam("助力用户token") String cloudAuthToken,
                              @RequestBody HelpUserInfoDto helpInfoDto) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                UserDto userDto = JsonXMLUtils.json2obj(redisUtil.getValue(cloudAuthToken + userId), UserDto.class);
                return helpService.help(userDto,helpInfoDto);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户未登录，请登录后重试");
    }
    /**
     * 用户查询助力人员名单
     * 运营查询助力人员名单时需传入活动参加人员的用户id
     * @param
     * @return
     */
    @GetMapping(value = "queryHelpedInfo",produces="application/json")
    @ApiOperation(value = "用户/运营查询助力人员名单")
    @ResponseBody
    public Result queryHelpedInfo(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                                  @RequestParam("aId")@ApiParam("活动id")String aId,
                                  @RequestParam(value = "card") @ApiParam("card:0--用户侧 1--运营侧")Integer card,
                                  @RequestParam(value = "uId",required = false) @ApiParam("运营查询助力人员名单时需传入活动参加人员的用户id") String uId,
                                  @RequestParam("type")@ApiParam("type:0--历史活动（可查看中奖名单等信息）1--正在进行的活动 2--预热活动") Integer type) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                if (card == 0) {
                    UserDto userDto = JsonXMLUtils.json2obj(redisUtil.getValue(cloudAuthToken + userId), UserDto.class);
                    return helpService.queryHelpedInfo(userDto,aId,type);
                }
                if (card == 1) {//运营
                    return helpService.queryHelpedInfo(new UserDto(),aId,type);
                }
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户名已存在，请重新输入");
    }

    /**
     * 用户查询某活动下拥有的抽奖号
     * @param
     * @return
     */
    @GetMapping(value = "queryHelpedPrizeNumInfo",produces="application/json")
    @ApiOperation(value = "用户查询某活动下拥有的抽奖号（舍弃）")
    @ResponseBody
    public Result queryHelpedPrizeNumInfo(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                                  @RequestParam("aId")@ApiParam("活动id")String aId,
                                  @RequestParam("type:0--历史活动（可查看中奖名单等信息）1--正在进行的活动 2--预热活动") Integer type) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                UserDto userDto = JsonXMLUtils.json2obj(redisUtil.getValue(cloudAuthToken + userId), UserDto.class);
                return helpService.queryHelpedPrizeNumInfo(userDto,aId,type);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户名已存在，请重新输入");
    }

    /**
     * 用户查询自己再某活动中助理过的所有用户
     * @param
     * @return
     */
    @GetMapping(value = "queryHelpInfo",produces="application/json")
    @ApiOperation(value = "用户查询自己再某活动中助力过的所有用户(舍弃)")
    @ResponseBody
    public Result queryHelpInfo(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                                @RequestParam("aId") @ApiParam("活动id") String aId) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return helpService.queryHelpInfo(userId,aId);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户名已存在，请重新输入");
    }
}
